package com.rivulet.dawn.service.service.archive.impl;


import com.rivulet.dawn.api.model.archive.PostDataModel;
import com.rivulet.dawn.api.request.AllDeleteRequest;
import com.rivulet.dawn.api.request.archive.PostDataSaveRequest;
import com.rivulet.dawn.api.request.archive.PostDataSearchRequest;
import com.rivulet.dawn.api.response.archive.PostDataDetailResponse;
import com.rivulet.dawn.api.response.archive.PostDataSearchResponse;
import com.rivulet.dawn.api.response.business.TagSearchResponse;
import com.rivulet.dawn.api.tools.EmptyTool;
import com.rivulet.dawn.api.tools.Page;
import com.rivulet.dawn.service.mapper.PostDataContentMapper;
import com.rivulet.dawn.service.mapper.PostDataMapper;
import com.rivulet.dawn.service.service.archive.PostDataService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class PostDataServiceImpl implements PostDataService {
    @Resource
    PostDataMapper postDataMapper;
    @Resource
    PostDataContentMapper postDataContentMapper;

    @Override
    public Integer save(PostDataSaveRequest request) {
        PostDataModel postDataModel = new PostDataModel();
        BeanUtils.copyProperties(request, postDataModel);
        //处理内容
        String tagIds = request.getTagIds().stream().map(String::valueOf).collect(Collectors.joining(","));
        postDataModel.setTagIds(tagIds);
        postDataMapper.insert(postDataModel);
        postDataContentMapper.insert(postDataModel.getId(), request.getContent());
        return 0;
    }

    @Override
    public void delete(AllDeleteRequest request) {
        if (Objects.nonNull(request) && !CollectionUtils.isEmpty(request.getIds())) {
            for (Long id : request.getIds()) {
                postDataMapper.deleteByPrimaryKey(id);
                postDataContentMapper.deleteByPrimaryKey(id);
            }
        }
    }

    @Override
    public Page<PostDataSearchResponse> search(PostDataSearchRequest request) {
        if (Objects.isNull(request)) {
            return EmptyTool.emptyPage();
        }
        Long total = postDataMapper.total(request);
        if (Objects.isNull(total) || total <= 0) {
            return EmptyTool.emptyPage();
        }
        List<PostDataSearchResponse> responseList = postDataMapper.search(request);
        return new Page<>(total, responseList);
    }

    @Override
    public PostDataDetailResponse detail(Long id) {
        return null;
    }
}
